package br.com.cfccultura.admin.control.actions.login;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Component;

import br.com.cfccultura.admin.control.actions.DefaultAction;
import br.com.cfccultura.admin.model.vo.AgendamentoProva;
import br.com.cfccultura.admin.model.vo.ContaReceber;
import br.com.cfccultura.admin.model.vo.Matricula;
import br.com.cfccultura.admin.model.vo.OrdemServico;
import br.com.cfccultura.admin.model.vo.Turma;
import br.com.cfccultura.admin.model.vo.ValueObject;
import br.com.cfccultura.admin.view.jsp.helper.AlunosPresenteComDebitoHelper;
import br.com.cfccultura.admin.view.jsp.helper.TurmasEmCursoHelper;

@Component("/homeAtendimento")
public class HomeAtendimentoAction extends DefaultAction {
	
	private static final Logger logger = Logger.getLogger(HomeAtendimentoAction.class); 
	
	public ActionForward carregarTurmasEmCurso(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		Turma vo = new Turma();
		vo.setInicio(new Date());
		vo.setTermino(new Date());
		List<Turma> turmas = getTurmaDAO().consultar(vo);
		
		if(turmas.size() > 0) {
			request.setAttribute("turmasEmCursoList", convertListHelper(turmas, TurmasEmCursoHelper.class));
		}
		
		return mapping.findForward("turmasEmCurso");
	}
	
	private List<?> convertListHelper(List<? extends ValueObject> list, Class<?> helper) throws Exception {
		
		List<Object> helpers = new ArrayList<Object>();
		
		for(ValueObject vo : list) {
			Object obj = helper.newInstance();
			BeanUtils.copyProperties(obj, vo);
			helpers.add(obj);
		}
		
		return helpers;
	}
	
	public ActionForward carregarProximasTurmas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		Turma vo = new Turma();
		vo.setInicio(new Date());
		List<Turma> turmas = getTurmaDAO().consultar(vo);
		
		if(turmas.size() > 0)
			request.setAttribute("proximasTurmasList", turmas);
		
		return mapping.findForward("proximasTurmas");
	}
	
	public ActionForward carregarAlunosPresentesDebito(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
			
		Turma vo = new Turma();
//		vo.setInicio(new Date());
//		vo.setTermino(new Date());
//		vo.setPeriodo(Periodo.MANHA);
		List<Turma> turmas = getTurmaDAO().consultar(vo);
		
		List<Matricula> matriculaDebito = new ArrayList<Matricula>();
		
		for(Turma turma : turmas) {
			m:for(Matricula matricula : turma.getMatriculas()) {
				List<ContaReceber> debitos = getFluxoCaixaDAO().verificaDebito(matricula.getOrdemServico());
				if(debitos.size() == 0)
					continue;
				
				OrdemServico os = getOrdemServicoDAO().consultar(debitos.get(0).getOrdemServicoEspecie().getOrdemServico().getCodigo());
				matricula.setOrdemServico(os);
				matricula.setTurma(turma);
				
				double totalPago = 0D;
				for(ContaReceber receber : debitos) {
					totalPago += receber.getValorPagamento();
				}
				if(totalPago < os.getTotal()) {
					os.setDevedor(os.getTotal() - totalPago);
					matriculaDebito.add(matricula);
				}				
			}				
		}
		if(matriculaDebito.size() > 0) {
			request.setAttribute("alunosPresentesDebitoList", convertListHelper(matriculaDebito, AlunosPresenteComDebitoHelper.class));
		}
		
		return mapping.findForward("alunosPresentesDebito");
	}
	
	public ActionForward carregarProvasDoDia(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		AgendamentoProva vo = new AgendamentoProva();
		
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		
		vo.setDataHora(c.getTime());

		List<AgendamentoProva> list = getAgendamentoProvaDAO().consultar(vo, true);
		for(AgendamentoProva prova : list) {
			OrdemServico os = getOrdemServicoDAO().consultar(prova.getOrdemServico().getCodigo());
			prova.setOrdemServico(os);
			List<ContaReceber> debitos = getFluxoCaixaDAO().verificaDebito(os);
			double totalPago = 0D;
			for(ContaReceber receber : debitos) {
				totalPago += receber.getValorPagamento();
			}
			if(totalPago < os.getTotal()) {
				os.setDevedor(os.getTotal() - totalPago);
			}
		}
		
		if(list.size() > 0)
			request.setAttribute("provasDoDiaList", list);
		
		return mapping.findForward("provasDoDia");
	}
}
